package dao.daocontrollers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import workerswithdb.IDAOMaster;
import workerswithdb.SortMastersBy;
import api.entitites.IMaster;
import entitiesfactory.EntitiesFactory;

public class DAOMaster extends GenericDAO implements IDAOMaster {

	private Connection connection;
	private PreparedStatement getOrderByMaster;
	private PreparedStatement sortMasters;

	private ResultSet masterFromDb_result;
	private ResultSet orderByMaster_result;;
	private ResultSet sortMasters_result;

	private static final String ORDER_BY_MASTER = "select number_order from orders join masters using(id_master) where master_name=?";
	private static final String SORT_MASTERS = "select master_name,employment_master from masters order by ?";

	private ArrayList<String> answer = new ArrayList<String>();
	private EntitiesFactory entities_factory;

	public DAOMaster() {
		super(IDAOMaster.class);
		try {
			entities_factory = EntitiesFactory.getInstance();
			connection = super.getConnection();
			getOrderByMaster = connection.prepareStatement(ORDER_BY_MASTER);
			sortMasters = connection.prepareStatement(SORT_MASTERS);

		} catch (SQLException sqle) {

		}
	}

	public ArrayList<String> insertMasterToDB(IMaster master) {
		answer.clear();

		try {
			super.insert(master.getNameMaster(), master.getEmploymentMaster());
			answer.add(DBMessanger.INSERT_OPERATION_SUCCESS);

		} catch (SQLException sqle) {
			answer.clear();
			answer.add(DBMessanger.MASTER_EXISTS);
		}
		return answer;
	}

	public IMaster getMasterFromDb(String master_name) {
		IMaster master = (IMaster) entities_factory.getObject(IMaster.class);
		try {
			masterFromDb_result = super.read(master_name);
			while (masterFromDb_result.next()) {
				String masterName = masterFromDb_result.getString(2);
				boolean free = masterFromDb_result.getBoolean(3);
				master.setNameMaster(masterName);
				if (!free) {
					master.markMaster();
				}
			}
		} catch (SQLException sqle) {
			System.out.println(DBMessanger.MASTER_NOT_EXISTS);

		}

		return master;
	}

	public ArrayList<String> SortMasters(SortMastersBy sortMastersBy) {
		answer.clear();
		try {
			sortMasters.setString(1, sortMastersBy.toString());
			sortMasters_result = sortMasters.executeQuery();
			while (sortMasters_result.next()) {
				String line = DBMessanger.MASTERS_NAME
						+ sortMasters_result.getString(1)
						+ DBMessanger.MASTERS_EMPLOYMENT
						+ sortMasters_result.getBoolean(2);
				answer.add(line);
			}

		} catch (SQLException sqle) {
			answer.clear();
			answer.add(DBMessanger.ERROR);

		}
		return answer;

	}

	public ArrayList<String> getOrderByMaster(String master_name) {
		answer.clear();
		try {
			Integer numberOrder = 0;
			getOrderByMaster.setString(1, master_name);
			orderByMaster_result = getOrderByMaster.executeQuery();
			while (orderByMaster_result.next()) {
				numberOrder = orderByMaster_result.getInt(1);
			}
			answer.add(numberOrder.toString());

		} catch (SQLException sqle) {
			answer.add(DBMessanger.ERROR_ORDER_BY_MASTER);

		}
		return answer;

	}

	public Integer getIdMaster(String master_name) {
		try {
			Integer id_master = super.getObjectId(master_name);
			return id_master;

		} catch (SQLException sqle) {
			System.out.println(DBMessanger.MASTER_NOT_EXISTS);
			return null;

		}
	}

	public ArrayList<String> deleteMaster(String master_name) {
		answer.clear();
		try {
			super.deleteObject(master_name);
			answer.add(DBMessanger.MASTER_DELETE);

		} catch (SQLException sqle) {
			answer.clear();
			answer.add(DBMessanger.ERROR);
		}
		return answer;
	}

}
